  import WxValidate from "../../utils/WxValidate";
  const app = getApp()
  Page({
    /**
     * 页面的初始数据
     */
    data: {
      auth_id: "",
      auth_pwd: "",
      identify: "",
      identify_image_url: `http://${app.globalData.target}//permission/getKaptcha`,
      image_arraybuffer: "",
      hello_code: ""
    },
    onLoad: function (options) {
        //校验数据
        this.initValidate()
        //加载图片
        this.loadImage()
    },
    /*登录 */
    login(e) {
      let formData = e.detail.value
      console.log(`formData:`)
      console.log(formData)
      console.log(`data:`)
      console.log(this.data)
      if (!this.WxValidate.checkForm(this.data)) {
        //表单元素验证不通过，此处给出相应提示
        let error = this.WxValidate.errorList[0];
        this.showModal(error);
        return false;
      }
      if(this.data.identify != this.data.hello_code){
        wx.showToast({
          title: '验证码错误',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      wx.request({
        url: `http://${app.globalData.target}//permission/login`,
        data: {
          userId:this.data.auth_id,
          password:this.data.auth_pwd,
          verifyCode:this.data.identify
        },
        method: 'GET',
        success: function(res){
          console.log(res)
          if (res.data.code != 0) {
            //登录失败
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000,
              success: function(){
                console.log('fail to login')
                //刷新验证码操作
              }
            })
          }else{
            //登录成功
            //存储用户信息
            //跳转页面
            function parseStrObjByRegExp(strDes){
              var obj = {}
              strDes.replace(/(\w+)(?:=([^;]*))?/g,function(str, key, value){
                obj[key] = value
              })
              return obj
            }
            var cookiesStr = res.cookies[0];
            var cookieObj = parseStrObjByRegExp(cookiesStr)
            console.log("cookieObj:")
            console.log(cookieObj)
            wx.setStorageSync("hello-token", cookieObj.token)
            wx.setStorageSync("user-information",res.data.result)
            // var info = wx.getStorageSync('user-information')
            // console.log(info)
            wx.switchTab({
              url: '../SearchModule/homePage',
            })
          }
        },
        fail: function() {
          console.log('fail to link')
        },
      })
  
    },
    loadImage() {
      var that = this
      wx.request({
        url: `${this.data.identify_image_url}?random=${parseInt(100*Math.random())}`,
        method: 'GET',
        responseType: 'arraybuffer',
        success: function(res){
          console.log(`imageRes:`)
          console.log(res)
          console.log(`imageRes.header.helloCode:${res.header['hello-code']}`)
          that.setData({
            image_arraybuffer: wx.arrayBufferToBase64(res.data),
            hello_code: res.header['hello-code']
          })
        }
      })
    },
  
    /*校验数据 */
    initValidate() {
      let rules = {
        auth_id: {
          required: true,
        },
        auth_pwd: {
          required: true,
        },
        identify: {
          required: true,
        }
      }
      //用于提示的信息
      let message = {
        auth_id: {
          required: '请输入用户名',
        },
        auth_pwd: {
          required: '请输入密码',
        },
        identify: {
          required: '请输入验证码',
        }
      }
      //实例化当前的验证规则和提示消息
      this.WxValidate = new WxValidate(rules, message);
    },
    /**错误提示 */
    showModal(error) {
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 1000
      })
    },
    bindReloadImage: function(e){
      console.log(e)
      var that = this
      wx.request({
        url: `${this.data.identify_image_url}?random=${parseInt(100*Math.random())}`,
        method: 'GET',
        responseType: 'arraybuffer',
        success: function(res){
          console.log(res)
          that.setData({
            image_arraybuffer: wx.arrayBufferToBase64(res.data),
            hello_code: res.header['hello-code']
          })
        }
      })
    },
    bindAuthIdChange: function(e){
      this.setData({
          auth_id: e.detail.value
      });
    },
    bindPwdChange: function(e){
      this.setData({
          auth_pwd: e.detail.value
      });
    },
    bindIdentifyChange: function(e){
      this.setData({
          identify: e.detail.value
      });
    },
  })